package com.weishuang.monitor.leader.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishuang.monitor.leader.commons.utils.FormatUtil;
import com.weishuang.monitor.leader.entity.ServerDiskInfo;
import com.weishuang.monitor.leader.entity.vo.ServerDiskInfoVo;
import com.weishuang.monitor.leader.entity.vo.ServerDiskUseVo;
import com.weishuang.monitor.leader.mapper.ServerDiskInfoMapper;
import com.weishuang.monitor.leader.service.IServerDiskInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务器磁盘信息 服务实现类
 * </p>
 *
 * @author weishuang
 * @since 2021-03-23
 */
@Service
public class ServerDiskInfoServiceImpl extends ServiceImpl<ServerDiskInfoMapper, ServerDiskInfo> implements IServerDiskInfoService {

    /**
     * 获取磁盘使用情况
     * @param sysId
     * @return
     */
    @Override
    public ServerDiskUseVo getDiskUseInfo(String sysId) {
        LambdaQueryWrapper<ServerDiskInfo> eq = Wrappers.lambdaQuery(ServerDiskInfo.class).eq(ServerDiskInfo::getSysId, sysId);
        List<ServerDiskInfo> serverDiskInfos = baseMapper.selectList(eq);
        //整理需要的信息
        Long total = 0l;
        Long free = 0l;
        if (serverDiskInfos != null && serverDiskInfos.size() > 0) {
            for (ServerDiskInfo serverDiskInfo : serverDiskInfos) {
                total += serverDiskInfo.getDiskSpace();
                free += serverDiskInfo.getDiskSpaceFree();
            }
        }
        ServerDiskUseVo result = new ServerDiskUseVo();
        result.setDiskSpace(FormatUtil.formatBytes(total));
        result.setDiskSpaceFree(FormatUtil.formatBytes(free));
        result.setDiskSpaceUse(FormatUtil.formatBytes(total - free));
        BigDecimal useRate = new BigDecimal("0");
        if (!total.equals(0l)) {
            useRate = new BigDecimal(String.valueOf(total - free)).divide(new BigDecimal(String.valueOf(total)),4,BigDecimal.ROUND_DOWN).multiply(new BigDecimal("100"));
        }
        result.setDiskUseRate(useRate);
        return result;
    }

    @Override
    public List<ServerDiskInfoVo> serverDiskInfoService(String sysId) {
        LambdaQueryWrapper<ServerDiskInfo> serverDiskInfoLambdaQueryWrapper = Wrappers.lambdaQuery(ServerDiskInfo.class).eq(ServerDiskInfo::getSysId, sysId).orderByAsc(ServerDiskInfo::getDiskMount);
        List<ServerDiskInfo> list = baseMapper.selectList(serverDiskInfoLambdaQueryWrapper);
        List<ServerDiskInfoVo> result = new ArrayList<>();
        for (ServerDiskInfo serverDiskInfo : list) {
            ServerDiskInfoVo serverDiskInfoVo = BeanUtil.copyProperties(serverDiskInfo, ServerDiskInfoVo.class,"diskSpace","diskSpaceFree");
            serverDiskInfoVo.setDiskSpace(FormatUtil.formatBytes(serverDiskInfo.getDiskSpace()));
            serverDiskInfoVo.setDiskSpaceFree(FormatUtil.formatBytes(serverDiskInfo.getDiskSpaceFree()));
            if (serverDiskInfo.getDiskSpace() != null && !serverDiskInfo.getDiskSpace().equals(0l)) {
                BigDecimal diskUseRate = new BigDecimal(String.valueOf(serverDiskInfo.getDiskSpace() - serverDiskInfo.getDiskSpaceFree())).divide(new BigDecimal(String.valueOf(serverDiskInfo.getDiskSpace())), 4, BigDecimal.ROUND_DOWN).multiply(new BigDecimal("100"));
                serverDiskInfoVo.setDiskUseRate(diskUseRate);
            }else{
                serverDiskInfoVo.setDiskUseRate(new BigDecimal("0"));
            }
            result.add(serverDiskInfoVo);
        }
        return result;
    }

    /**
     * 先删除所有磁盘，然后再新增
     * @param sysId
     * @param serverDiskInfos
     */
    @Override
    public void upServerDiskInfo(String sysId, List<ServerDiskInfo> serverDiskInfos) {
        LambdaQueryWrapper<ServerDiskInfo> eq = Wrappers.lambdaQuery(ServerDiskInfo.class).eq(ServerDiskInfo::getSysId, sysId);
        baseMapper.delete(eq);
        this.saveBatch(serverDiskInfos);
    }

}
